using System.Data;
using UwinEducation.Application.Dtos;
using UwinEducation.Domain.Entities;

namespace UwinEducation.Application.Extensions
{
    public static class TeacherScheduleExtensions
    {
        public static List<TeacherSchedule> GenerateSchedules(this BatchCreateTeacherScheduleDto input)
        {
            var schedules = new List<TeacherSchedule>();
            var currentDate = input.StartTime.Date;
            var endDate = input.EndTime.Date;
            var guid = Guid.NewGuid();

            // 遍历日期范围内的每一天
            while (currentDate <= endDate)
            {
                // 获取当前日期是星期几 (0-6, 0=星期日)
                int currentDayOfWeek = (int)currentDate.DayOfWeek;

                // 检查是否有匹配的时间规则
                var matchingRules = input.CreateTeacherScheduleRulesDtos
                    .Where(r => r.DayOfWeek == currentDayOfWeek)
                    .ToList();

                // 为每个匹配的规则创建课表
                foreach (var rule in matchingRules)
                {
                    var startTime = currentDate.Add(rule.StartTime);
                    var endTime = currentDate.Add(rule.EndTime);

                    // 创建课表记录
                    var schedule = TeacherSchedule.Create(
                        input.TeacherId,
                        input.CourseId,
                        input.ClassroomId,
                        input.UnitPrice,
                        input.Capacity,
                        startTime,
                        endTime,
                        guid,
                        input.IsForceCheck,
                        rule.Remarks??string.Empty
                    );
                    schedule.CourseType = rule.CourseType;

                    schedules.Add(schedule);
                }

                currentDate = currentDate.AddDays(1);
            }

            return schedules;
        }

      
    }
} 